package com.example.mydiary;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Calendar;
import java.util.TimeZone;


public class DiaryDetail extends AppCompatActivity {
    private MyDiaryDatabase mdb; //定制好的数据库操作类
    private EditText body;  //内容
    private Button btn1,btn2,btn3;
    private int nid;//数据记录的主键
    private int flag;//修改是1，添加是0
    private TextView time;
    private String t;//时间
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diary_detail);
        setTitle(R.string.edit_diary);
        time=(TextView) findViewById(R.id.time);
        body=(EditText)findViewById(R.id.d_body);

        btn1 = (Button)findViewById(R.id.btn1);
        btn2 = (Button)findViewById(R.id.btn2);
        btn3 = (Button)findViewById(R.id.btn3);

        btn1.setOnClickListener(new mClick());
        btn2.setOnClickListener(new mClick());
        btn3.setOnClickListener(new mClick());

        mdb = new MyDiaryDatabase(this);
        mdb.opendb();

        flag=(int)this.getIntent().getExtras().getInt("flag");
        if(flag==1){
            nid=(int)this.getIntent().getExtras().getInt("ID");
            init(nid);
        }else{
            body.setText("");
            gettime();
        }
    }
    private void gettime(){//获取系统当前时间，取到分钟
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("GTM+8:00"));
        String year=String.valueOf(cal.get(Calendar.YEAR));
        String month=String.valueOf(cal.get(Calendar.MONTH)+1);
        String day=String.valueOf(cal.get(Calendar.DATE));
        String hour=String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
        String min=String.valueOf(cal.get(Calendar.MINUTE));
        String sec=String.valueOf(cal.get(Calendar.SECOND));
        if(month.length()<2)month="0"+month;
        if(day.length()<2)day="0"+day;
        if(hour.length()<2)hour="0"+hour;
        if(min.length()<2)min="0"+min;//规范格式，长度为1时往前补0
        t=year+"年"+month+"月"+day+"日"+hour+":"+min;
        time.setText(t);
        cal.clear();
        cal.set(Integer.valueOf(year),Integer.valueOf(month),Integer.valueOf(day),Integer.valueOf(hour),Integer.valueOf(min),Integer.valueOf(sec));
        //String t2=String.valueOf(cal.getTimeInMillis());//秒
    }

    private void init(int nid) {
        Cursor cursor = mdb.getdata(nid);
        if (cursor != null) {
           if(cursor.moveToFirst()){
               body.setText(cursor.getString(0));
               time.setText(cursor.getString(1));
           }
        }
        cursor.close();
    }
    //设置“保存”、“删除”、“返回”按钮点击事件
    class mClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            if(v==btn1){//“保存”
                ContentValues cv=new ContentValues();
                gettime();
                cv.put(mdb.BODY,body.getText().toString());
                cv.put(mdb.TIME,time.getText().toString());

                if(flag==1)//修改备忘录
                {
                    mdb.update(cv,nid);
                }
                else{//添加备忘录
                    mdb.insertdata(cv);
                    flag=1;//已添加的备忘录再按“保存”按钮要变为修改备忘录
                }
                Toast.makeText(DiaryDetail.this,"保存成功",Toast.LENGTH_SHORT).show();

            }
            else if(v==btn2){//“返回”
                Intent i=new Intent(DiaryDetail.this,DiaryListActivity.class);
                startActivity(i);
            }
            else if(v==btn3){//“删除”
                mdb.deletedata(nid);
                Intent i=new Intent(DiaryDetail.this,DiaryListActivity.class);
                startActivity(i);
            }
        }

    }
}

